package com.core.gui.main;

import java.awt.EventQueue;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.core.dao.DAOFactory;
import com.core.dao.impl.DBUtil;
import com.core.entities.Contact;
import com.core.gui.dialog.AddContact;
import com.core.gui.dialog.ChangePasswordDialog;
import com.core.gui.dialog.ContactSearch;
import com.core.gui.dialog.LoginDialog;
import com.core.gui.dialog.RegistrationDialog;
import com.core.gui.helper.UserInfo;
import com.core.gui.helper.model.ContactTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private final JMenuBar menuBar = new JMenuBar();
	private final JMenu mnFile = new JMenu("File");
	private final JMenuItem mntmLogOut = new JMenuItem("Log Out");
	private final JMenuItem mntmExit = new JMenuItem("Exit");
	private final JMenuItem ChangePasswordMenuItem = new JMenuItem("Change Password");
	private final JMenu mnTools = new JMenu("Tools");
	private final JMenuItem addContactsMenuItem = new JMenuItem("AddContacts");
	private JTable contactTable = new JTable(new ContactTableModel(new ArrayList<Contact>()));
	private final JScrollPane scrollPane = new JScrollPane();
	private final JMenuItem searchMntm = new JMenuItem("Search");


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
	
		try {

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			// handle exception
		} catch (ClassNotFoundException e) {
			// handle exception
		} catch (InstantiationException e) {
			// handle exception
		} catch (IllegalAccessException e) {
			// handle exception
		}
		if(UserInfo.getInstance().getService().getUsernamSrv().isAdminCreatedBefor()){
			new LoginDialog().setVisible(true);
		}else{
			new RegistrationDialog().setVisible(true);
		}
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					MainFrame frame = new MainFrame();
//					frame.setVisible(true);
//					
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
		
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 636, 621);
		{
			setJMenuBar(menuBar);
		}
		{
			menuBar.add(mnFile);
		}
		{
			ChangePasswordMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					new ChangePasswordDialog().setVisible(true);
				}
			});

			mnFile.add(ChangePasswordMenuItem);
		}
		{
			mnFile.add(mntmLogOut);
		}
		{
			mnFile.add(mntmExit);
		}
		{
			menuBar.add(mnTools);
		}
		{
			addContactsMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					new AddContact().setVisible(true);
				}
			});

			mnTools.add(addContactsMenuItem);
		}
		{
			searchMntm.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					new ContactSearch().setVisible(true);
				}
			});
			mnTools.add(searchMntm);
		}
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		{
			scrollPane.setAutoscrolls(true);
			scrollPane.setViewportBorder(null);
			scrollPane.setBounds(10, 11, 360, 540);
			contentPane.add(scrollPane);
		}
		{
			
			scrollPane.setViewportView(contactTable);
			contactTable.setAutoscrolls(true);

			contactTable.getSelectionModel().addListSelectionListener(new RowListener());
			getContactTableData();
			
		}
	}
	
		private void getContactTableData() {
			Connection conn = DBUtil.getConnection();
		try{
			List<Contact> contacts = DAOFactory.getInstance().getContactDAO(conn).selectAll();
		
			contactTable.setModel(new ContactTableModel(contacts));
			
			}catch(Exception e){
				
			}
	
	}
private class RowListener implements ListSelectionListener {
    public void valueChanged(ListSelectionEvent event) {
        if (event.getValueIsAdjusting()) {
            return;
        }
//        if(event.getFirstIndex()>=0){
//            preparePatientUI(preparePatientFromTable());
//            updateButton.setEnabled(true);
//            deleteButton.setEnabled(true);
//            historyButton.setEnabled(true);
//        }else{
//            updateButton.setEnabled(false);
//            deleteButton.setEnabled(false);
//            historyButton.setEnabled(false);
//        }
    }
}		
}

